package com.arrogames.blondbattle.game.purchases 
{
	import adobe.utils.ProductManager;
	import ash.core.Engine;
	import ash.core.NodeList;
	import ash.core.System;
	import com.arrogames.blondbattle.game.combat.Faction;
	import com.arrogames.blondbattle.game.graphics.ui.PurchaseButton;
	import com.arrogames.blondbattle.game.purchases.Cooldown;
	import com.arrogames.blondbattle.game.purchases.Order;
	import com.arrogames.blondbattle.game.EntityCreator;
	import com.arrogames.blondbattle.game.state.CommanderNode;
	import com.arrogames.blondbattle.game.purchases.CooldownNode;
	import com.arrogames.blondbattle.game.purchases.OrderNode;
	import com.arrogames.blondbattle.model.UnitDefinition;
	import org.as3commons.logging.api.getLogger;
	import org.as3commons.logging.api.ILogger;
	
	/**
	 * ...
	 * @author Robin van den Bogaard
	 */
	public class OrderSystem extends System
	{
		
		private static const logger:ILogger = getLogger(OrderSystem);
		
		private var orders:NodeList;
		private var commanders:NodeList;
		private var cooldowns:NodeList;
		private var purchaseButtons:NodeList;
		
		private var creator:EntityCreator;
		
		public function OrderSystem(creator:EntityCreator)
		{
			super();
			this.creator = creator;
		}
		
		override public function update(time:Number):void
		{
			super.update(time);
			
			var order:OrderNode;
			var commander:CommanderNode;
			for (order = orders.head; order; order = order.next ) {
				
				commander = getCommander(order.faction.name);
				if (commander != null) {
					if (!onCooldown(order.unit.name, commander.faction.name)) {
						if (commander.money.value >= order.order.unit.costs) {
							logger.debug("{0} purchasing '{1}'.", [commander.faction.name, order.order.unit.name]);
							creator.createUnit(order.order.unit, commander.faction);
							commander.money.value -= order.order.unit.costs;
							
							creator.createCooldown(order.unit, order.order.unit.cooldown, commander.faction);
							setPurchaseButtonCooldown(order.unit.name, commander.faction);
							order.entity.add(new Cooldown(order.order.unit.cooldown));
						} else {
							logger.debug("{0} does not have enough money to purchase '{1}' (has {2}; required {3}).",
								[commander.faction.name, order.order.unit.name, commander.money.value, order.order.unit.costs]);
						}
					} else {
						logger.debug("{0} has '{1}' on cooldown.", [commander.faction.name, order.order.unit.name]);
					}
				} else {
					logger.debug("Unknown faction placed an order: {0}.", [order.faction.name]);
				}
				
				creator.destroyEntity(order.entity);
			}
		}
		
		private function onCooldown(unit:String, faction:String):Boolean
		{
			var result:Boolean = false;
			var node:CooldownNode;
			
			for (node = cooldowns.head; node && !result; node = node.next) {
				result = node.faction.name == faction && node.unit.name == unit;
			}
			return result;
		}
		
		private function getCommander(faction:String):CommanderNode
		{
			var node:CommanderNode;
			var result:CommanderNode = null;
			for (node = commanders.head; node && result == null; node = node.next ) {
				if (node.faction.name == faction) {
					result = node;
				}
			}
			return result;
		}
		
		private function setPurchaseButtonCooldown(unit:String, faction:Faction) : void 
		{
			//check if the faction is human, cooldown for the purchase button is not applicable to the AI faction
			if (faction.name == Faction.HUMAN) {
				var node:PurchaseButtonNode;
				for (node = purchaseButtons.head; node; node = node.next ) {
					if ( node.unit.name == unit ) {
						//gotcha, now we can set the cooldown on the interface button
						var view:PurchaseButton = node.display.displayObject as PurchaseButton;
						view.startCooldownAnimation();
					}
				}
			}
		}
		
		override public function addToEngine(engine:Engine):void
		{
			super.addToEngine(engine);
			orders = engine.getNodeList(OrderNode);
			commanders = engine.getNodeList(CommanderNode);
			cooldowns = engine.getNodeList(CooldownNode);
			purchaseButtons = engine.getNodeList(PurchaseButtonNode);
		}
		
		override public function removeFromEngine(engine:Engine):void
		{
			super.removeFromEngine(engine);
			orders = null;
			commanders = null;
		}
	}

}